package zkk.com.mengqu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import zkk.com.mengqu.R;
import zkk.com.mengqu.adapter.HandbookAdapter;

/**
 * 指南页面
 */
public class HandbookFragment extends Fragment implements android.view.View.OnClickListener{
    private Context context;
    private ViewPager pager;
    private View view;
    private LinearLayout location;//定位
    private ImageView search;//搜索
    private HandbookAdapter adapter;
    private TextView kitchenShare,privateMenu;//厨房分享，私人菜谱

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_handbook, container,false);
        context = getContext();
        init();
        return view;
    }

    private void init(){
        kitchenShare = (TextView)view.findViewById(R.id.tx_kitchen_share);
        privateMenu = (TextView)view.findViewById(R.id.tx_private_menu);

        location = (LinearLayout) view.findViewById(R.id.ll_location);
        search = (ImageView) view.findViewById(R.id.iv_search);

        pager = (ViewPager)view.findViewById(R.id.game_viewPager);
        pager.setOffscreenPageLimit(2);
        pager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_margin_width));
        pager.setPageMarginDrawable(R.color.gray_background);
        //滑动监听
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                setActionBarTabSelected(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        adapter = new HandbookAdapter(getChildFragmentManager());
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);
        kitchenShare.setSelected(true);

        kitchenShare.setOnClickListener(this);
        privateMenu.setOnClickListener(this);
        location.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tx_kitchen_share:
                setActionBarTabSelected(0);
                break;
            case R.id.tx_private_menu:
                setActionBarTabSelected(1);
                break;
            case R.id.ll_location:
                Toast.makeText(context , "定位" , Toast.LENGTH_LONG).show();
                break;
            case R.id.iv_search:
                Toast.makeText(context , "搜索" , Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

    private void setActionBarTabSelected(int paramInt){
        pager.setCurrentItem(paramInt);
        if(paramInt == 0){
            kitchenShare.setSelected(true);
            privateMenu.setSelected(false);

            kitchenShare.setTextColor(getResources().getColor(R.color.black));
            privateMenu.setTextColor(getResources().getColor(R.color.tab_menu_text));
        }else if(paramInt==1){
            privateMenu.setSelected(true);
            kitchenShare.setSelected(false);

            privateMenu.setTextColor(getResources().getColor(R.color.black));
            kitchenShare.setTextColor(getResources().getColor(R.color.tab_menu_text));
        }
    }
}
